unit StatEntry;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DATAEDIT, StdCtrls, Grids, Mask;

type
  TfmStatEntry = class(TForm)
    sgData: TStringGrid;
    btnSave: TButton;
    Label1: TLabel;
    mskDate: TMaskEdit;
    Label2: TLabel;
    cmbOtdel: TComboBox;
    procedure sgDataKeyPress(Sender: TObject; var Key: Char);
    procedure btnSaveClick(Sender: TObject);
    procedure mskDateExit(Sender: TObject);
    procedure mskDateKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure cmbOtdelChange(Sender: TObject);
  private
    { Private declarations }
    procedure ClearData;
    procedure DisplayData;
    function GetOtdel: integer;
    procedure SaveData;
  public
    { Public declarations }
  end;

var
  fmStatEntry: TfmStatEntry;

implementation
uses DicDM, DataModule;
{$R *.DFM}

procedure TfmStatEntry.FormCreate(Sender: TObject);
begin
  if dmData.IsConnected then begin
    mskDate.Text := FormatDateTime('dd/mm/yyyy', Date());
    with dmDic.qryTmp do begin
      Close;
      SQL.Text := 'SELECT * FROM STAT_TYPES WHERE VISIBLE = 1 ORDER BY ID';
      Open;
      sgData.Cols[0].Clear;
      while not Eof do begin
        sgData.RowCount := RecNo;
        sgData.Cols[0].AddObject(FieldByName('NAME').AsString,
                                  pointer(FieldByName('ID').AsInteger));
        Next;
      end;
      Close;
    end;
    cmbOtdel.Items.Assign(dmData.FOtdels);
    cmbOtdel.Items.Delete(cmbOtdel.Items.Count - 1);
    cmbOtdel.ItemIndex := cmbOtdel.Items.IndexOfObject(pointer(dmData.FOtdel));
  end;
end;

procedure TfmStatEntry.sgDataKeyPress(Sender: TObject; var Key: Char);
begin
  with Sender as TStringGrid do
    if Key = Chr(VK_RETURN) then
      if Row < RowCount - 1 then
        Row := Row + 1;
end;

procedure TfmStatEntry.btnSaveClick(Sender: TObject);
begin
  SaveData;
  ClearData;
  mskDate.SetFocus;
end;

procedure TfmStatEntry.DisplayData;
var i, stat_type: integer;
begin
  ClearData;
  with dmDic.qryTmp do begin
    Close;
    SQL.Clear;
    SQL.Add('SELECT * FROM STAT WHERE STAT_DATE = :stat_date');
    SQL.Add('AND OTDEL = :otdel');
    ParamByName('stat_date').AsString := mskDate.Text;
    ParamByName('otdel').AsInteger := GetOtdel;
    Open;
    while not Eof do begin
      stat_type := FieldByName('STAT_TYPE').AsInteger;
      i := sgData.Cols[0].IndexOfObject(pointer(stat_type));
      if i <> - 1 then
        sgData.Cols[1].Strings[i] := FieldByName('STAT_VALUE').AsString;
      Next;
    end;
    Close;
  end;
end;

procedure TfmStatEntry.SaveData;
var
  i, stat_type, stat_value: integer;
  d: TDateTime;
begin
  d := StrToDate(mskDate.Text);
  with dmDic.qryTmp do begin
    if Transaction.InTransaction then Transaction.Commit;
    Transaction.StartTransaction;
    try
      SQL.Clear;
      SQL.Add('DELETE FROM STAT WHERE STAT_DATE = :stat_date');
      SQL.Add('AND OTDEL = :otdel');
      ParamByName('stat_date').AsString := mskDate.Text;
      ParamByName('otdel').AsInteger := GetOtdel;
      ExecSQL;
      for i := 0 to sgData.RowCount - 1 do begin
        stat_type := integer(sgData.Cols[0].Objects[i]);
        stat_value := StrToIntDef(sgData.Cols[1].Strings[i], 0);
        SQL.Text := 'INSERT INTO STAT (OTDEL, STAT_DATE, STAT_TYPE, STAT_VALUE) ' +
          Format('VALUES (%d, ''%s'', %d, %d) ', [GetOtdel, mskDate.Text, stat_type, stat_value]);
        ExecSQL;
      end;
      Transaction.Commit;
    except
      Transaction.RollBack;
    end;
  end;
end;

procedure TfmStatEntry.ClearData;
begin
  sgData.Cols[1].Clear;
end;

procedure TfmStatEntry.mskDateExit(Sender: TObject);
begin
  DisplayData;
end;

procedure TfmStatEntry.mskDateKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    SelectNext(ActiveControl, True, True);
end;

function TfmStatEntry.GetOtdel: integer;
begin
  result := integer(cmbOtdel.Items.Objects[cmbOtdel.ItemIndex]);
end;

procedure TfmStatEntry.cmbOtdelChange(Sender: TObject);
begin
  DisplayData;
end;

end.
